using System;
using System.Text.RegularExpressions;

namespace Prologis.ShifterNT.Converter
{

    public class StringMatchConverter : IConverter
    {


        /// <summary>
        /// 
        /// </summary>
        /// 
        string fieldName = null;
        public string FieldName
        {
            get { return fieldName; }
            set { fieldName = value; }
        }


        /// <summary>
        /// 
        /// </summary>
        /// 
        bool convertAllFields = true;
        public bool ConvertAllFields
        {
            get { return convertAllFields; }
            set { convertAllFields = value; }
        }


        /// <summary>
        /// 
        /// </summary>
        /// 
        bool trimStartAllFields = true;
        public bool TrimStartAllFields
        {
            get { return trimStartAllFields; }
            set { trimStartAllFields = value; }
        }
        /// <summary>
        /// 
        /// </summary>
        /// 
        string stringToMatch = null;
        public string StringToMatch
        {
            get { return stringToMatch; }
            set { stringToMatch = value; }
        }


        /// <summary>
        /// 
        /// </summary>
        /// 
        Regex regex = null;
        string regexToMatch;
        public string RegexToMatch
        {
            get { return regexToMatch; }
            set
            {
                regexToMatch = value;
                regex = new Regex(regexToMatch, RegexOptions.Compiled);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// 
        string replaceString = null;
        public string ReplaceString
        {
            get { return replaceString; }
            set { replaceString = value; }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        /// 
        public FieldList Convert(FieldList data)
        {
            if (stringToMatch == null && regex == null)
            {
                return data;
            }

            if (regex != null)
            {
                throw new ArgumentNullException("ConverterRegex not implemented");
            }
            else if (stringToMatch != null)
            {
                if (convertAllFields)
                {                    

                    for (int i = 0; i < data.Count; i++)
                    {
                        
                        string fieldValue = data[i].Value.ToString();
                        string convertedField = fieldValue.Replace(stringToMatch, replaceString);
                        
                        data[i].Value = convertedField;
                    }
                }
                else
                {
                    if (fieldName != null)
                    {
                        data[fieldName].Value = data[fieldName].Value.ToString().Replace(stringToMatch, replaceString);
                    }
                    else
                    {
                        throw new ArgumentNullException("Fieldname must not be null");
                    }
                }

                if (trimStartAllFields)
                {
                    for (int i = 0; i < data.Count; i++)
                    {
                        data[i].Value = data[i].Value.ToString().TrimStart();
                    }
                }
                else
                {
                    if (fieldName != null)
                    {
                        data[fieldName].Value = data[fieldName].Value.ToString().TrimStart();
                    }
                    else
                    {
                        throw new ArgumentNullException("Fieldname must not be null");
                    }
                }

            }
            return data;

        }
    }



}
